// pages/service/service.jscon
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageRoot: app.globalData.imageRoot,
    // state: 3,
    show: false,
    checked: false,
    checked1: false,
    checked2: false,
    checked3: false,
    checked4: false,
    checked5: false,
    createArr: [],
    actions: [{
      value: 0,
      name: '不限'
    }, {
      value: 1,
      name: '待服务'
    }, {
      value: 2,
      name: '服务中'
    }, {
      value: 3,
      name: '待评价'
    }, {
      value: 4,
      name: '已完成'
    }],
    listAll: [],
    urgeUserphone: '',
    index: 0,
    myl: false,
    x_stage: 0,
    page_token: 0,
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  handbtnstatus() {
    this.setData({
      show: true
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  //筛选
  onSelect(event) { //筛选
    // console.log(event.currentTarget.dataset)
    var that = this;
    let fuilterId = event.currentTarget.dataset.value; //筛选选中
    that.setData({
      x_stage: fuilterId
    })
    that.setData({
      page_token: 0
    }) //重置页码
    let index = that.data.index; //订单类型
    let data = {
      page_size: 10,
      page_token: 0,
      stage: that.data.x_stage,
      ordertype: index
    } //订单类型默认为0及所有
    that.api(data);
    this.setData({
      show: false
    });
  },
  api(data) {
    var that = this;
    getApp().ports.apiYyallUser({
      data
    }).then((res) => {
      let datalist = res.data;

      if (that.data.Loading) {
        wx.hideLoading({
          success: (res) => {},
        })
      }
      datalist.results.forEach(item => {
        switch (item.type) {
          case 2:
            item.banner = that.data.imageRoot + item.banner;
            break;
          case 3:
            item.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg'
            item.title = item.clientName+' '+item.title +' ' + item.builtUpAreaSquareCentimeter+'㎡'+' '+item.intentionPriceYuan +'万'
            break;
          case 5:
            let total_budget_price_yuan = ['200万以下','200-500万','500-100万','1000万以上'];
            let house_area = ['50㎡以下','50㎡-90㎡','90㎡-144㎡',,'144㎡以上'];
            item.banner = 'https://api.yipaiju.com/resources/images/wawa/hf.png'
            item.title = item.clientName+' ' + item.province + item.district + ' ' + total_budget_price_yuan[item.totalBudgetPriceYuan] + ' ' + house_area[item.houseArea]
            break;
          case 6:
            let order_types = ['', '个人转贷',' 企业转贷'];
            let budget_price_yuan = ['50万以下','50万-150万','150万-300万','300万以上'];
            item.banner = 'https://api.yipaiju.com/resources/images/wawa/zz.jpg'
            item.title =  item.clientName+' ' + order_types[item.orderType] + ' ' + budget_price_yuan[item.budgetPriceYuan]
            break;
        }
        item.time = item.createTime.substring(0, 10);
        item.url = '../serviceEval/serviceEval?id=' + item.id
        if (data.page_token >= 1) {
          that.data.listAll.results.push(item)
        }
        
      })
      if (data.page_token >= 1) {
        datalist.results = that.data.listAll.results
      }
       




      // if (data.page_token >= 1) {
      //   if (that.data.Loading) {
      //     wx.hideLoading({
      //       success: (res) => {},
      //     })
      //   }
      //   datalist.results.forEach(item => {
      //     switch (item.type) {
      //       case 3:
      //         item.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg'
      //         item.title = item.client_name+' '+item.title +' ' + item.built_up_area_square_centimeter+'㎡'+' '+item.intention_price_yuan +'万'
      //         break;
      //       case 5:
      //         let total_budget_price_yuan = ['200万以下','200-500万','500-100万','1000万以上'];
      //         let house_area = ['50㎡以下','50㎡-90㎡','90㎡-144㎡',,'144㎡以上'];
      //         item.banner = 'https://api.yipaiju.com/resources/images/wawa/hf.png'
      //         item.title = item.client_name+' ' + item.province + item.district + ' ' + total_budget_price_yuan[item.total_budget_price_yuan] + ' ' + house_area[item.house_area]
      //         break;
      //       case 6:
      //         let order_types = ['个人转贷',' 企业转贷'];
      //         let budget_price_yuan = ['50万以下','50万-150万','150万-300万','300万以上'];
      //         item.banner = 'https://api.yipaiju.com/resources/images/wawa/zz.jpg'
      //         item.title =  item.client_name+' ' + order_types[item.order_type] + ' ' + budget_price_yuan[item.budget_price_yuan]
      //         break;
      //     }
      //     item.time = item.create_time.substring(0, 10);
      //     item.url = '../serviceEval/serviceEval?id=' + item.id
      //     that.data.listAll.results.push(item)
      //   })

      //   datalist.results = that.data.listAll.results
      // } else {
      //   datalist.results.forEach(item => {
      //     switch (item.type) {
      //       case 3:
      //         item.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg'
      //         item.title = item.client_name+' '+item.title +' ' + item.built_up_area_square_centimeter+'㎡'+' '+item.intention_price_yuan +'万'
      //         break;
      //       case 5:
      //         let total_budget_price_yuan = ['200万以下','200-500万','500-100万','1000万以上'];
      //         let house_area = ['50㎡以下','50㎡-90㎡','90㎡-144㎡',,'144㎡以上'];
      //         item.banner = 'https://api.yipaiju.com/resources/images/wawa/hf.png'
      //         item.title = item.client_name+' ' + item.province + item.district + ' ' + total_budget_price_yuan[item.total_budget_price_yuan] + ' ' + house_area[item.house_area]
      //         break;
      //       case 6:
      //         let order_types = ['个人转贷',' 企业转贷'];
      //         let budget_price_yuan = ['50万以下','50万-150万','150万-300万','300万以上'];
      //         item.banner = 'https://api.yipaiju.com/resources/images/wawa/zz.jpg'
      //         item.title =  item.client_name+' ' + order_types[item.order_type] + ' ' + budget_price_yuan[item.budget_price_yuan]
      //         break;
      //     }
      //     item.time = item.create_time.substring(0, 10);
      //     item.url = '../serviceEval/serviceEval?id=' + item.id
      //   })
      // }


      // 需要显示用户的信息
      // that.marketInfo(datalist)
      that.setData({
        listAll: datalist,
        myl: false
      })
    }).catch(() => {
      console.error('出啥错了？');
    });
  },
  marketInfo(datalist) {
    let that = this;
    //获取用户信息
    let liky = datalist.results.filter((p) => {
      return p.marketers
    })
    let v = [];
    for (let i = 0; i < liky.length; i++) {
      v[i] = liky[i].marketers[0].id
    }
    console.log(v, "===============vvvvv")
    // return
    getApp().ports.api38({
      data: v
    }).then((res) => {
      console.log(res.data)
      for (let i in datalist.results) {
        for (let j in res.data) {
          if (datalist.results[i].marketers) {
            if (datalist.results[i].marketers[0].id == res.data[j].id) {
              datalist.results[i].img = res.data[j].avatar_url;
              datalist.results[i].markName = res.data[j].nickname
              datalist.results[i].telphone = res.data[j].phone
            }
          }
        }
      }
      that.setData({
        listAll: datalist
      })
      // console.log(datalist)
    }).catch(() => {
      console.error('批量获取用户电话号码！')
    })
  },
  scroll(e) { //预约顶大分类
    var that = this;
    let index = e.detail.index;
    this.setData({
      page_token: 0
    }) //页码
    this.setData({
      x_stage: 0
    }) //订单状态 0为全部
    this.setData({
      index: index
    }) //订单类型 0为全部
    let data = {
      ordertype: index,
      page_size: 10,
      stage: that.data.x_stage,
      page_token: 0
    };
    that.api(data)
  },
  onLoad: function (options) { //页面初始加载
    wx.hideShareMenu({
      menus: ['shareAppMessage', 'shareTimeline']
    })
    let data = {
      ordertype: 0,
      stage: 0,
      page_size: 10,
      page_token: this.data.page_token
    }
    this.api(data);
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: app.globalData.color,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.btn = this.selectComponent("#cellbox");
    // this.btn1 = this.selectComponent("#cellbox1");
    // this.btn2 = this.selectComponent("#cellbox2");
    // this.btn3 = this.selectComponent("#cellbox3");
    // this.btn4 = this.selectComponent("#cellbox4");
    // this.btn5 = this.selectComponent("#cellbox5");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {

  // },
  onReachBottom() { //上拉加载更多
    var that = this;
    //检查是否还有数据可以加载
    var total_size = that.data.listAll.total_size; //数据总数
    var size = that.data.listAll.results.length; //当前数组长度，及数据
    var page_token = that.data.page_token += 1; //每次下拉，页码加1
    //如果还有，则继续加载
    if (total_size > size) {
      var data = {
        ordertype: that.data.index,
        page_token: page_token,
        stage: that.data.x_stage,
        page_size: 10
      }
      that.setData({
        Loading: that.data.Loading,
        page_token: page_token
      })
      that.data.Loading = wx.showLoading({
        title: '加载中',
      })
      that.api(data)
      // console.log(size, data)
      //如果没有了，则停止加载，显示没有更多内容了
    } else {
      that.setData({
        myl: true
      })
    }
  },

})

//接受数据过来的数据
//  getAddInfo(e) {
//   this.setData({ urgeUserphone : e.detail })
//   // console.log(e.detail)
//   if (e.detail.length < this.data.listAll.results.length) {
//     this.setData({  checked: false });
//   } else if (e.detail.length = this.data.listAll.results.length) {
//     this.setData({checked: true });
//   }
// },
// getAddInfo1(e) {
//   this.setData({ urgeUserphone : e.detail })
//   // console.log(e.detail)
//   if (e.detail.length < this.data.listAll.results.length) {
//     this.setData({
//       checked1: false
//     });
//   } else if (e.detail.length = this.data.listAll.results.length) {
//     this.setData({
//       checked1: true
//     });
//   }
// },
// getAddInfo2(e) {
//   this.setData({ urgeUserphone : e.detail })
//   // console.log(e.detail)
//   if (e.detail.length < this.data.listAll.results.length) {
//     this.setData({
//       checked2: false
//     });
//   } else if (e.detail.length = this.data.listAll.results.length) {
//     this.setData({
//       checked2: true
//     });
//   }
// },
// getAddInfo3(e) {
//   this.setData({ urgeUserphone : e.detail })
//   // console.log(e.detail)
//   if (e.detail.length < this.data.listAll.results.length) {
//     this.setData({
//       checked3: false
//     });
//   } else if (e.detail.length = this.data.listAll.results.length) {
//     this.setData({
//       checked3: true
//     });
//   }
// },
// getAddInfo4(e) {
//   this.setData({ urgeUserphone : e.detail })
//   // console.log(e.detail)
//   if (e.detail.length < this.data.listAll.results.length) {
//     this.setData({
//       checked4: false
//     });
//   } else if (e.detail.length = this.data.listAll.results.length) {
//     this.setData({
//       checked4: true
//     });
//   }
// },
// getAddInfo5(e) {
//   this.setData({ urgeUserphone : e.detail })
//   // console.log(e.detail)
//   if (e.detail.length < this.data.listAll.results.length) {
//     this.setData({
//       checked5: false
//     });
//   } else if (e.detail.length = this.data.listAll.results.length) {
//     this.setData({
//       checked5: true
//     });
//   }
// },



// onChange(event) {
//   // console.log(event)
//   this.setData({ checked: event.detail, })
// },
// onChange1(event) {
//   this.setData({
//     checked1: event.detail,
//   });
// },
// onChange2(event) {
//   this.setData({
//     checked2: event.detail,
//   });
// },
// onChange3(event) {
//   this.setData({
//     checked3: event.detail,
//   });
// },
// onChange4(event) {
//   this.setData({
//     checked4: event.detail,
//   });
// },
// onChange5(event) {
//   this.setData({
//     checked5: event.detail,
//   });
// },
// botton() {
//   // console.log(this.data.checked)
//   if (this.data.checked == true) {
//     this.btn.queryall();
//   } else {
//     this.btn.del();
//   };
//   if (this.data.checked1 == true) {
//     this.btn1.queryall();
//   } else {
//     this.btn1.del();
//   };
//   if (this.data.checked2 == true) {
//     this.btn2.queryall();
//   } else {
//     this.btn2.del();
//   };
//   if (this.data.checked3 == true) {
//     this.btn3.queryall();
//   } else {
//     this.btn3.del();
//   }
//   if (this.data.checked4 == true) {
//     this.btn4.queryall();
//   } else {
//     this.btn4.del();
//   }
//   if (this.data.checked5 == true) {
//     this.btn5.queryall();
//   } else {
//     this.btn5.del();
//   }
// },